package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.wf.captcha.SpecCaptcha;
import io.jsonwebtoken.Claims;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Autowired
    SystemUserMapper systemUserMapper;

    @Override
    public CaptchaVo getCaptcha() {

        // 生成验证码
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48,4);
        String text = specCaptcha.text().toLowerCase();// 文本
        String img = specCaptcha.toBase64();
        String key = UUID.randomUUID().toString().replace("-","");

        // 保存缓存
        redisTemplate.opsForValue().set(key,text,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);


        CaptchaVo captchaVo = new CaptchaVo(img,key);
        return captchaVo;
    }

    @Override
    public String login(LoginVo loginVo) {
        // 验证码
        String captchaKey = loginVo.getCaptchaKey();
        String userCode = loginVo.getCaptchaCode();
        String textCache = redisTemplate.opsForValue().get(captchaKey);
        Assert.notNull(textCache,"验证码不存在");
        Assert.isTrue(userCode.equals(textCache),"验证码错误");

        // 用户名密码
        String username = loginVo.getUsername();
        String password = loginVo.getPassword();
        String userPassword = DigestUtils.md5Hex(password);
        QueryWrapper<SystemUser> systemUserQueryWrapper = new QueryWrapper<>();
        systemUserQueryWrapper.eq("username",username);
        SystemUser systemUser = systemUserMapper.selectOne(systemUserQueryWrapper);
        Assert.notNull(systemUser,"用户不存在");
        String passwordDB = systemUser.getPassword();
        Assert.isTrue(userPassword.equals(passwordDB),"密码错误");

        // 生成令牌
        String token = JwtUtil.createToken(systemUser.getId(),systemUser.getUsername());

        return token;
    }

    @Override
    public SystemUserInfoVo getInfo(String access_token) {
        Claims claims = JwtUtil.parseToken(access_token);
        Long userId = claims.get("userId",Long.class);
        SystemUser systemUser = systemUserMapper.selectById(userId);
        SystemUserInfoVo systemUserInfoVo = new SystemUserInfoVo();
        systemUserInfoVo.setName(systemUser.getName());
        systemUserInfoVo.setAvatarUrl(systemUser.getAvatarUrl());
        return systemUserInfoVo;
    }

}
